import time

import aio_pika
from fastapi import APIRouter
from common.g.g import G_ID_WORKER, G_LOGGER, G_MQ


router = APIRouter(prefix="/test")

@router.get("/id_worker")
def generate_id():
    for i in range(1000):
        new_id = G_ID_WORKER.get_id()
        G_LOGGER.info(f"新的id:{new_id}")
    return "ok"


@router.get("/rabbitmq")
async def rabbitmq_test():
    async with G_MQ.acquire() as chan:  # type: aio_pika.abc.AbstractChannel
        message = aio_pika.Message(body=f"创建未支付订单! {time.time()}".encode())
        exchange = await chan.declare_exchange(name="mall_order", durable=True)
        await exchange.publish(message, routing_key="order")
    return "ok"